package com.jiankangjin.transsystem.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.trading.entity.IsTipOrdeAcountEntity;
import com.jiankangjin.trading.entity.MerBusiTypeInfoEntity;
import com.jiankangjin.trading.entity.MerFeeTypeInfoEntity;
import com.jiankangjin.trading.entity.MerInfoEntity;
import com.jiankangjin.trading.entity.MerOperUserAffiliatedEntity;
import com.jiankangjin.trading.entity.MerOperUserConfEntity;
import com.jiankangjin.trading.entity.MerPermInfoEntity;
import com.jiankangjin.transsystem.services.GetIsTipOrderServices;
import com.jiankangjin.transsystem.services.MerInfoServices;
import com.jiankangjin.transsystem.services.MerOperUserAffiliatedServices;
import com.jiankangjin.transsystem.services.MerOperUserConfServices;
import com.jiankangjin.transsystem.vo.GetIsTipOrderVo;
import com.jiankangjin.transsystem.vo.GetIsTipOrderVo.MerInfo;


/**  
 * 类功能说明 ：用户登录后，获取商户列表信息：获取用户操作员的权限，商户名，获取商户未补全的订单数量，获取商户支持的业务类型，获取商户支持商品类型以及获取商户的头像
 * @author zfy 
 * @date 2014年12月18日 下午3:26:43 
 * @version V1.0 
 */
@Controller
public class GetIsTipOrderController {
	private static final Log log = LogFactory.getLog(CreateOrderController.class);

	@Autowired
	private GetIsTipOrderServices getIsTipOrderServices;

	@Autowired
	private MerOperUserConfServices merOperUserConfServices;
	
	@Autowired
	private MerOperUserAffiliatedServices merOperUserAffiliatedServices;

	@Autowired
	private MerInfoServices merInfoServices;

	@RequestMapping(value = "/getIsTipOrder", method = RequestMethod.POST)
	@ResponseBody
	public GetIsTipOrderVo getOrderData(
			@RequestParam(required = false) String operatorId) {
		if (log.isInfoEnabled()) {
			StringBuilder sb = new StringBuilder();
			sb.append("operatorId=").append(operatorId);
			log.info("调用接口 " + sb);
		}
		GetIsTipOrderVo getIsTipOrderVo = new GetIsTipOrderVo();
		try {
			// 获取用户的权限，用于客户端权限判定（退款是否显示，是否可以审批）
			MerPermInfoEntity merPermInfoEntity = new MerPermInfoEntity();
			merPermInfoEntity.setOperatorId(operatorId);
			List<MerPermInfoEntity> list = (List<MerPermInfoEntity>) getIsTipOrderServices
					.getMerPermInfo(merPermInfoEntity);
			GetIsTipOrderVo.MerPermInfo[] mrPermInfoSz = new GetIsTipOrderVo.MerPermInfo[list
					.size()];
			for (int i = 0; i < list.size(); i++) {
				GetIsTipOrderVo.MerPermInfo merPermInfoDx = getIsTipOrderVo.new MerPermInfo();
				MerPermInfoEntity merPermInfo = (MerPermInfoEntity) list.get(i);
				merPermInfoDx.setPermCde(merPermInfo.getPermCde());
				merPermInfoDx.setPermName(merPermInfo.getPermName());
				mrPermInfoSz[i] = merPermInfoDx;
			}
			getIsTipOrderVo.setMerPermInfo(mrPermInfoSz);
			// 获取商户的信息
			MerOperUserConfEntity merOperUserConfEntity = new MerOperUserConfEntity();
			merOperUserConfEntity.setOperUserId(operatorId);
			List<MerOperUserConfEntity> merOperUserList = (List<MerOperUserConfEntity>) merOperUserConfServices.getMerOperUserConfInfo(merOperUserConfEntity);
			GetIsTipOrderVo.MerInfo[] merInfo1 = new GetIsTipOrderVo.MerInfo[merOperUserList.size()];
			for (int i = 0; i < merOperUserList.size(); i++) {
				GetIsTipOrderVo.MerInfo merInfo = getIsTipOrderVo.new MerInfo();
				// 获取商户号  及商户操作员关系表id
				MerOperUserConfEntity MerOperUserConfInfo = merOperUserList.get(i);
				String merId = (String) MerOperUserConfInfo.getMerId();
				String merOperUserconfId =(String)MerOperUserConfInfo.getId();
				// 获取商户名称
				MerInfoEntity merInfoEntity = new MerInfoEntity();
				merInfoEntity.setMerId(merId);
				MerInfoEntity MerInfo = (MerInfoEntity) merInfoServices.getMerInfo(merInfoEntity);
				if(MerInfo == null){
				    continue;
				}
				String merName = (String)MerInfo.getMerName();
	            merInfo.setMerName(merName);
				// 获取商户未补全的订单数量
				IsTipOrdeAcountEntity isTipOrdeAcountEntity = new IsTipOrdeAcountEntity();
				isTipOrdeAcountEntity.setMerId(merId);
				IsTipOrdeAcountEntity isTipOrdeAcount = (IsTipOrdeAcountEntity) getIsTipOrderServices.getIsTipOrdeAcount(isTipOrdeAcountEntity);
				if (isTipOrdeAcount == null) {
					merInfo.setIsTipAcount("0");
				} else {
					merInfo.setIsTipAcount(isTipOrdeAcount.getIsTipAcount());
				}
				// 获取商户支持的业务类型
				MerBusiTypeInfoEntity merBusiTypeInfoEntity = new MerBusiTypeInfoEntity();
				merBusiTypeInfoEntity.setMerId(merId);
				List<MerBusiTypeInfoEntity> merBusiTypeInfoList = (List<MerBusiTypeInfoEntity>) getIsTipOrderServices.getMerBusiTypeInfo(merBusiTypeInfoEntity);
				GetIsTipOrderVo.MerInfo.MerBusiType[] merBusiTypesz = new GetIsTipOrderVo.MerInfo.MerBusiType[merBusiTypeInfoList.size()];
				for (int k = 0; k < merBusiTypeInfoList.size(); k++) {
					GetIsTipOrderVo.MerInfo.MerBusiType MerBusiTypedy = getIsTipOrderVo.new MerInfo().new MerBusiType();
					MerBusiTypeInfoEntity merBusiTypeInfoRes = (MerBusiTypeInfoEntity) merBusiTypeInfoList.get(k);
					MerBusiTypedy.setBusl_type(merBusiTypeInfoRes.getBusiType());
					MerBusiTypedy.setBusl_type_name(merBusiTypeInfoRes.getBusiTypeName());
					
					//根据商户号 和 商户业务类型 获取商户的业务类型所对应的商品类型
					MerFeeTypeInfoEntity merFeeTypeInfo = new MerFeeTypeInfoEntity();
					merFeeTypeInfo.setMerId(merId);
					merFeeTypeInfo.setMerBuslType(merBusiTypeInfoRes.getBusiType());
					List<MerFeeTypeInfoEntity> merFeeTypeInfoList = (List<MerFeeTypeInfoEntity>) getIsTipOrderServices.getMerFeeTypeInfo(merFeeTypeInfo);
					GetIsTipOrderVo.MerInfo.MerBusiType.FeeTypeInfo[] feeTypeInfosz = new GetIsTipOrderVo.MerInfo.MerBusiType.FeeTypeInfo[merFeeTypeInfoList.size()];
					for (int t = 0; t < merFeeTypeInfoList.size(); t++) {
						GetIsTipOrderVo.MerInfo.MerBusiType.FeeTypeInfo feeTypeInfody = getIsTipOrderVo.new MerInfo().new MerBusiType().new FeeTypeInfo();
						MerFeeTypeInfoEntity merFeeTypeInfoRes = (MerFeeTypeInfoEntity) merFeeTypeInfoList.get(t);
						feeTypeInfody.setFee_type(merFeeTypeInfoRes.getFeeType());
						feeTypeInfody.setFee_type_name(merFeeTypeInfoRes.getFeeTypeName());
						feeTypeInfosz[t] = feeTypeInfody;
					}
					MerBusiTypedy.setFeeTypeInfo(feeTypeInfosz);		
					merBusiTypesz[k] = MerBusiTypedy;
				}
               //获取用户所在商户的头像
				MerOperUserAffiliatedEntity merOperUserAffiliatedEntity = new MerOperUserAffiliatedEntity();
				merOperUserAffiliatedEntity.setMeroperuserconfId(merOperUserconfId);
				MerOperUserAffiliatedEntity merOperUserAffiliated  = (MerOperUserAffiliatedEntity) merOperUserAffiliatedServices.getMerOperUserAffiliatedInfo(merOperUserAffiliatedEntity);
				if(merOperUserAffiliated != null){
				merInfo.setUserLogo(merOperUserAffiliated.getUserLogo());
				}
				merInfo.setMerBusiType(merBusiTypesz);
				merInfo1[i] = merInfo;
			}
			getIsTipOrderVo.setMerInfo(merInfo1);
			getIsTipOrderVo.setResp_status("0000");
			getIsTipOrderVo.setMsg("获取商户列表信息成功！");
			log.info("获取商户列表信息成功！");

		} catch (Exception e) {
			e.printStackTrace();
			getIsTipOrderVo.setMsg("系统错误！");
			getIsTipOrderVo.setResp_status("9999");
			log.info("交易系统错误！");
			return getIsTipOrderVo;
		}
		return getIsTipOrderVo;
	}
}
